package cn.elead.chaos.framework.web.global.cors;

import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import cn.elead.chaos.framework.properties.Cross;

@Deprecated
public class CorsUtils {

	/**
	 * 全局跨域支持
	 * 
	 * @param registry
	 */
	public static void addCorsMappings(CorsRegistry registry, Cross cross) {
		// https://segmentfault.com/a/1190000018554528?utm_source=tag-newest
		// 添加映射路径,允许跨域访问的路径
		registry.addMapping("/**")
				// 放行哪些原始域,允许跨域访问的源
				.allowedOrigins("*")
				// 是否发送Cookie信息
				.allowCredentials(true)
				// 放行哪些原始域,允许请求方法(请求方式)
				.allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
				// 放行哪些原始域,允许头部设置(头部信息)
				.allowedHeaders("Content-Type", "x-token", "Authorization", "sso_sessionid")
				// 暴露哪些头部信息（因为跨域访问默认不能获取全部头部信息）
				.exposedHeaders("x-token", "Authorization", "sso_sessionid").maxAge(3600);
	}


	/**
	 * SpringBoot 2.x Vue Axios 跨域问题及401等异常处理
	 * 
	 * @param registry
	 */
	public static void addInterceptors(InterceptorRegistry registry, Cross cross) {
		CrossInterceptor crossInterceptor = new CrossInterceptor();
		crossInterceptor.setCross(cross);
		registry.addInterceptor(crossInterceptor).addPathPatterns(cross.getPathPatterns());
		registry.addInterceptor(new OptionsInterceptor()).addPathPatterns(cross.getPathPatterns());
	}
}
